################################################################################  
###
###	INTERTEMPORAL EVIDENCE ON THE STRATEGY OF POPULISM IN THE US (Gennaro, Lecce, Morelli)
###	Replication Package
###  
### Figure 2
###
################################################################################  
  

library(dplyr)
library(ggplot2)
library(ggpubr)
library(gridExtra)
library(cowplot)
library(patchwork)

# Indicate location of the replication package here
wd <- ("Replication Package/")

###### NB: these estimates are obtain in tabA21.do ################################################# 

lab <- c('Core Voters', 'Core Voters', 'Core Voters', 'Non Core Voters', 'Non Core Voters', 'Non Core Voters')
lab2 <- c('Intention to Vote', 'Reported Turnout', 'Validated Turnout',
          'Intention to Vote', 'Reported Turnout', 'Validated Turnout')
coeff <- c( -.0188128 ,  -.0055475   ,-.0004392 , .0273378  , .0216864, .024917 )
std <- c(.005438, .0049166, .0039227, .0096831, .0080918, .0086675)
n <- c('N=2171', 'N=1995','N=1587',  'N=2277', 'N=2064', 'N=1569')
n2 <- c('Mean DV=0.94', 'Mean DV=0.95', 'Mean DV=0.96', 'Mean DV=0.81', 'Mean DV=0.82', 'Mean DV=0.85')


df <-  as.data.frame(cbind(lab, lab2, coeff, std, n, n2))
df$coeff <- as.numeric(as.character(df$coeff))
df$std <- as.numeric(as.character(df$std))
df$p <- df$coeff  %>% round(digits=3)
df$std_c = paste0('[', round(df$std, digits=3), ']')
names(df) <- c('lab', 'measure', 'e', 's', 'n', 'n2', 'p', 'std_c')

###### Plot   ################################################################## 

ggplot(df, aes(y = measure, x = e, size=3)) +
  geom_point(aes(y = measure, x = e), colour="black", size=2) +
  geom_errorbarh(aes(xmax = e+s*qnorm(.05,lower.tail=FALSE), xmin = e-s*qnorm(.05,lower.tail=FALSE)), height = 0.1, size=.5)  +
  geom_text(aes(label = p), nudge_y = 0.4, size=3) + 
  geom_text(aes(label = std_c), nudge_y = 0.3, size=3) + 
  geom_text(aes(label = n), nudge_y = 0.2, size=3) + 
  geom_text(aes(label = n2), nudge_y = 0.12, size=3) + 
  geom_vline(xintercept = 0, linetype="dotted")  + 
  scale_y_discrete(limits = rev(unique(sort(df$measure)))) +
  xlab('') + 
  ylab('') +
  facet_wrap(~lab, scales = "free_x")+
  theme_bw() +
  theme(strip.text.x = element_text(size=12, hjust = 0.5),
        strip.background = element_blank(),
        panel.grid.major.x = element_blank())


ggsave(paste0(wd, "4_results/figures/fig2.png"), width = 20, height = 12, units = "cm")



